/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lct.eq2.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Me
 */
public class ServerSocketThread extends Thread {
    private boolean open;
    Socket socket;
    SocketServer parent;
    
    public ServerSocketThread() {
    }
    
    public ServerSocketThread(Socket sock, SocketServer parent) {
        this.parent = parent;
        //System.out.println("ServerSocketThread::socket port = " + sock.getPort());
        this.open = true;
        this.socket = sock;
    }
    
    public void closeSocket() {
        this.open = false;
    }
    
    @Override
    public void run() {
        ObjectInputStream is = null;
        try {
            is = new ObjectInputStream(this.socket.getInputStream());
            Object o = is.readObject();
            this.parent.parent.addDataToTail(o);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
                socket.close();
            } catch (IOException ex) {
                Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
}
